package com.yyk.yiliao.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yyk.yiliao.R;
import com.yyk.yiliao.bean.Grid_Info;
import com.yyk.yiliao.ui.home.activity.AnZhengZhaoYao2_Activity;
import com.yyk.yiliao.ui.home.activity.Nearby_Activity;
import com.yyk.yiliao.util.ebs.AzzyEvent;
import com.yyk.yiliao.util.rx.ApiService;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * GridView加载数据adapter
 */

public class GvAdapter extends BaseAdapter {
    private List<Grid_Info> list;
    private LayoutInflater inflater;

    private Context context;

    private int mIndex;//页数下标，表示第几页，从0开始
    private int mPagerSize;//每页显示的最大数量

    public GvAdapter(List<Grid_Info> list, Context context, int mIndex, int mPagerSize) {
        this.list = list;
        this.context = context;
        this.mIndex = mIndex;
        this.mPagerSize = mPagerSize;
        inflater = LayoutInflater.from(context);
    }

    /**
     * 先判断数据集的大小是否足够显示满本页？listData.size() > (mIndex + img1)*mPagerSize
     * 如果满足，则此页就显示最大数量mPagerSize的个数
     * 如果不够显示每页的最大数量，那么剩下几个就显示几个 (listData.size() - mIndex*mPagerSize)
     */
    @Override
    public int getCount() {
        return list.size() > (mIndex + 1) * mPagerSize ? mPagerSize : (list.size() - mIndex * mPagerSize);
    }

    @Override
    public Object getItem(int position) {
        return list.get(position + mIndex * mPagerSize);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.gridview_adapter, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //重新确定postion(因为拿到的是总的数据源，数据源是分页加载到每页的GridView上的，为了确保正确的点对应不同页面上的Item)
        final int pos = position + mIndex * mPagerSize;//假设mPagerSize=8,假如点击的是第二页（即mIndex=img1）上的第二个位置item(position=img1),那么这个item的实际位置就是pos=9
        Grid_Info info = list.get(pos);
        holder.tv.setText(info.getName());
        if (pos == 0) {
            holder.img.setImageResource(R.mipmap.icon_fjyf);
        } else {

            Glide.with(context).load(ApiService.BASE_URL + info.getImgUrl()).into((ImageView) holder.img);
        }

        com.orhanobut.logger.Logger.e(ApiService.BASE_URL + info.getImgUrl() + "____");
        //添加item监听
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pos == 0) {
                    Intent intent = new Intent(context, Nearby_Activity.class);
                    context.startActivity(intent);
                } else {
                    Intent intent = new Intent(context, AnZhengZhaoYao2_Activity.class);
                    EventBus.getDefault().postSticky(new AzzyEvent(list.get(pos).getId() + "", pos-1 + ""));
                    context.startActivity(intent);
                }


//                Toast.makeText(context, "你点击了" + list.get(pos).getName(), Toast.LENGTH_SHORT).show();
            }
        });

        return convertView;
    }

    private class ViewHolder {
        private ImageView img;
        private TextView tv;

        public ViewHolder(View item) {
            tv = item.findViewById(R.id.tv);
            img = item.findViewById(R.id.img);

        }
    }

}
